package cn.lastjune.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 
 * @author Ryan
 *
 */
public class PropertiesUtil {

	private static Properties props;

	private static Properties propsGlobal;

	private static Properties countrylist;

	private static byte[] lockedProps = new byte[1];

	private static byte[] lockedPropsGlobal = new byte[1];

	private static String rootDomain;

	private static String appCa;

	private static String caUrlProtocol;

	private static String caUrlPort;

	private static String cmsUrlProtocol;

	private static String cmsUrlPort;

	private static void loadResource() {
		// the file in the class path
		try {
			synchronized (lockedProps) {
				if (null == props) {
					props = PropertiesLoaderUtils.loadAllProperties("application.properties");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param key specify a key
	 * @return a value.
	 */
	public static String getResourceByKey(final String key) {
		if (null == props) {
			loadResource();
		}
		return props.getProperty(key, "");
	}

	/**
	 * @param key specify a key
	 * @return a value.
	 */
	public static String getResourceByKey2(final String filename, final String key) {

		loadResource2(filename);

		return countrylist.getProperty(key, "");
	}

	/**
	 * @param propertyFileName
	 */
	private static void loadResource(final String propertyFileName) {
		// the file in the class path
		try {
			synchronized (lockedPropsGlobal) {
				if (null == propsGlobal) {
					propsGlobal = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * load the new file all the time.
	 * @param propertyFileName
	 */
	private static void loadResource2(final String propertyFileName) {
		// the file in the class path
		try {

			countrylist = PropertiesLoaderUtils.loadAllProperties(propertyFileName);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param key specify a key
	 * @param propertyFileName Property File Name
	 * @return a value.
	 */
	public static String getResourceByKey(final String propertyFileName, final String key) {

		loadResource(propertyFileName);

		return propsGlobal.getProperty(key, "");
	}

	/**
	 * get url protocol
	 * 
	 * @param appName web app name contraction
	 * @return URL Protocol
	 */
	public static String getUrlProtocol(String appName) {
		String defProtocol = "http";
		if ("ca".equals(appName)) {
			if (StringUtils.isBlank(caUrlProtocol)) {
				String t = getResourceByKey("ca.url.protocol");
				if (StringUtils.isNotBlank(t)) {
					caUrlProtocol = t.trim();
				} else {
					caUrlProtocol = defProtocol;
				}
			}
			return caUrlProtocol;
		} else if ("cms".equals(appName)) {
			if (StringUtils.isBlank(cmsUrlProtocol)) {
				String t = getResourceByKey("cms.url.protocol");
				if (StringUtils.isNotBlank(t)) {
					cmsUrlProtocol = t.trim();
				} else {
					cmsUrlProtocol = defProtocol;
				}
			}
			return cmsUrlProtocol;
		}
		return null;
	}

	/**
	 * get url port
	 * 
	 * @param appName web app name contraction
	 * @return URL Port
	 */
	public static String getUrlPort(String appName) {
		String defPort = "8080";
		if ("ca".equals(appName)) {
			if (StringUtils.isBlank(caUrlPort)) {
				String t = getResourceByKey("ca.url.port");
				if (StringUtils.isNotBlank(t)) {
					caUrlPort = t.trim();
				} else {
					caUrlPort = defPort;
				}
			}
			return caUrlPort;
		} else if ("cms".equals(appName)) {
			if (StringUtils.isBlank(cmsUrlPort)) {
				String t = getResourceByKey("cms.url.port");
				if (StringUtils.isNotBlank(t)) {
					cmsUrlPort = t.trim();
				} else {
					cmsUrlPort = defPort;
				}
			}
			return cmsUrlPort;
		}
		return null;
	}

	/**
	 * @return ""
	 */
	public static String getRootDomain() {
		String defDomain = ".6connex.com";
		if (StringUtils.isBlank(rootDomain)) {
			String t = getResourceByKey("root.domain");
			if (StringUtils.isNotBlank(t)) {
				rootDomain = t.trim();
			} else {
				rootDomain = defDomain;
			}
		}
		return rootDomain;
	}//

	/**
	 * @return ""
	 */
	public static String getAppClientAdmin() {
		String defApp = "clientadmin";
		if (StringUtils.isBlank(appCa)) {
			String t = getResourceByKey("app.clientadmin");
			if (StringUtils.isNotBlank(t)) {
				appCa = t.trim();
			} else {
				appCa = defApp;
			}
		}
		return appCa;
	}
}
